package com.demoku.web.application.test.impl;

import java.util.List;

import javax.inject.Named;

import org.iff.infra.util.BeanHelper;
import org.iff.infra.util.MapHelper;
import org.iff.infra.util.mybatis.plugin.Page;
import org.iff.infra.util.mybatis.service.Dao;
import org.springframework.transaction.annotation.Transactional;

import com.demoku.web.application.test.UserApplication;
import com.demoku.web.application.test.vo.UserVO;
import com.demoku.web.domain.test.User;

/**
 * @author autoCode
 */
@Named("userApplication")
@Transactional
public class UserApplicationImpl implements UserApplication {

	@Override
	public void removeUser(String userId) {
		User.remove(userId);
	}

	@Override
	public UserVO saveUser(UserVO vo) {
		User a = new User();
		BeanHelper.copyProperties(a, vo);
		a.save();
		UserVO copyProperties = BeanHelper.copyProperties(new UserVO(), a);
		return copyProperties;
	}

	@Override
	public UserVO getUserVO(String userId) {
		return BeanHelper.copyProperties(new UserVO(), User.get(userId));
	}

	@Override
	public List<UserVO> findAll(Page pages) {
		List<User> list = Dao.queryList("User_Mapping.findAll",
				MapHelper.toMap("page", pages));
		pages.setRows(list);
		return pages.toPage(UserVO.class).getRows();
	}

	@Override
	public Page pageFindAll(Page pages) {
		List<User> list = Dao.queryList("User_Mapping.findAll",
				MapHelper.toMap("page", pages));
		pages.setRows(list);
		return pages.toPage(UserVO.class);
	}
	
	@Override
	public Page pageFindAllUser(UserVO vo, Page pages) {
		
		List<User> list = Dao.queryList("User_Mapping.pageFindUser",
				MapHelper.toMap("page", pages , "name", vo.getName()));
		pages.setRows(list);
		return pages.toPage(UserVO.class);
	}

}
